﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Icms.Core.Model;
using Icms.Core.Repository;
using Omu.Awesome.Mvc;

namespace Icms.WebUI.Controllers
{
    public class CaseTypesLookupController : LookupController
    {
        private readonly IRepo<CaseType> repo;
        private const int pageSize = 10;

        public CaseTypesLookupController(IRepo<CaseType> repo)
        {
            this.repo = repo;
        }

        [HttpPost]
        public ActionResult Search(string search, IEnumerable<Guid> selected, int page)
        {
            var src = repo.Where(o => o.Title.Contains(search));
            if (selected != null) src = src.Where(o => !selected.Contains(o.Id));
            //(o => selected == null || !selected.Contains(o.Id)).ToList();

            var rows = this.RenderView(@"Awesome\LookupList", src.Skip((page - 1) * pageSize).Take(pageSize));
            return Json(new { rows, more = src.Count() > page * pageSize });
        }

        public ActionResult Selected(IEnumerable<Guid> selected)
        {
            var result = selected != null ? repo.Where(o => selected.Contains(o.Id)) : Enumerable.Empty<CaseType>();
            return View(@"Awesome\LookupList", result);
        }

        public ActionResult GetMultiple(IEnumerable<Guid> selected)
        {
            return Json(repo.GetAll().Where(o => selected.Contains(o.Id)).Select(v => new { Text = v.Title }));
        }
    }
}